//
//  RadioSortTableViewController.m
//  NEWS_W_Y_S
//
//  Created by lanou3g on 15/10/15.
//  Copyright © 2015年 lanou3g.jake. All rights reserved.
//

#import "RadioSortTableViewController.h"

#import "RadioSortCell.h"
#import "RadioSortModel.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "SortHelp.h"

#define RadioSort @"http://c.3g.163.com/nc/topicset/ios/radio/%@/%d-20.html"
static int  i = 20;

static NSString *radioSortCell = @"radioSortCell";

@interface RadioSortTableViewController ()

@property(nonatomic,strong)NSMutableArray *allRadioSortArray;

@property(nonatomic,strong)MBProgressHUD *progressHUD;


@end

@implementation RadioSortTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self lauoutHUD];
    self.allRadioSortArray = [NSMutableArray array];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"RadioSortCell" bundle:nil] forCellReuseIdentifier:radioSortCell];
    //请求数据
    [self requestData];
    //添加刷新
    [self addRefresh];
 
    self.navigationItem.title = self.cname;
}

-(void)requestData{

    [[SortHelp shareSortHelp] requestWithUrl:[NSString stringWithFormat:RadioSort,self.cid,0] block:^(NSMutableArray *array) {
        self.allRadioSortArray = array;
        [self.tableView reloadData];
    }];

}

-(void)addRefresh{
    
    //下拉
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [self.tableView.header beginRefreshing];
    //上拉
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefrsh)];
    
}
-(void)lauoutHUD{
    
    self.progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
    self.progressHUD.center = self.view.center;
    self.progressHUD.minSize = CGSizeMake(100, 100);
    self.progressHUD.alpha = 1;
    self.progressHUD.labelText = @"正在拼命加载中...";
    self.progressHUD.square = YES;//设置显示框的高度和宽度一样
    self.progressHUD.frame = self.view.bounds;
    [self.tableView addSubview:self.progressHUD];
    [self.progressHUD show:YES];
    
}

-(void)headerRefresh{
  //  NSLog(@"下拉刷新");
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self requestData];
        [self.tableView.header endRefreshing];
    });
    
}

-(void)footerRefrsh{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[SortHelp shareSortHelp]requestWithUrl:[NSString stringWithFormat:  RadioSort,self.cid,i] block:^(NSMutableArray *array) {
            
            [self.allRadioSortArray addObjectsFromArray:array];
            
            [self.tableView reloadData];
            i += 20;
          [self.tableView.footer endRefreshing];
            
        }];
        
    });
    
}
-(void)back:(UIBarButtonItem *)barbuttonItem{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%ld",self.allRadioSortArray.count);
    return self.allRadioSortArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RadioSortCell *cell = [tableView dequeueReusableCellWithIdentifier:radioSortCell forIndexPath:indexPath];
    RadioSortModel *model = self.allRadioSortArray[indexPath.row];
    [cell radioSortData:model];
    
    [self.progressHUD hide:YES];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

@end
